home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2007 July & August
/
PCWorld_2007-07-08_cd.bin
/
komunikace
/
maxthon
/
mcombo.exe
/
Plugin
/
Del.icio.us Sidebar
/
scripts
/
Delicious.js
next >
Wrap
Text File
|
2006-09-02
|
11KB
|
441 lines
/**************************
Del.icio.us Sidebar
By Neo101
Version 1.1.0
**************************/
checkSettingsFile()
var userName = decodeURIComponent( decodeTxt( readSetting('userName') ) )
var password = decodeURIComponent( decodeTxt( readSetting('password') ) )
var showTooltip = readSetting('showTooltip', '1')
var autoUpdateTime = parseInt( readSetting('autoUpdateTime', '0') )
var sortTags = readSetting('sortTags', '1')
var baseURL = 'https://api.del.icio.us/v1/'
var favoritesCache = 'cache/favorites.html'
var favoritesHTML = ''
var posts = new Array()
var xmlhttp
var editElement
var isAddFavorite = true
window.onload = function(){
if(userName == '' || password == ''){
displaySettings()
document.getElementById('menu').innerHTML = 'Del.icio.us Sidebar'
document.getElementById('settingsLinks').style.display = 'none'
document.getElementById('links').style.display = 'none'
} else {
viewFavorites()
getLastUpdateTime()
setLinks()
}
setSettingsForm()
setAutoUpdate()
}
function setLinks(){
document.getElementById('yourFavoritesLink').href = 'http://del.icio.us/' + userName
document.getElementById('yourNetworkLink').href = 'http://del.icio.us/network/' + userName
document.getElementById('importBookmarksLink').href = 'https://secure.del.icio.us/settings/' + userName + '/bookmarks/import'
document.getElementById('exportBookmarksLink').href = 'https://secure.del.icio.us/settings/' + userName + '/bookmarks/export'
}
function setAutoUpdate(){
if(isNaN(autoUpdateTime) || autoUpdateTime < 5){
return
}
var time = autoUpdateTime * 1000 * 60
window.setInterval('getLastUpdateTime()', time)
}
function loadXMLDoc(url, type, stateChange, data){
if(userName == '' || password == ''){
showError('User information not defined!')
return
}
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(err){
showError('Could not load ActiveX object')
}
xmlhttp.onreadystatechange = stateChange
xmlhttp.open("GET", url, true, userName, password)
xmlhttp.setRequestHeader('User-Agent', 'Maxthon del.icio.us sidebar plugin 1.0')
xmlhttp.send(null)
}
function checkReadyState(obj){
if(obj.readyState == 4){
if(obj.status == 200 || obj.status == 201){
return true;
}
else if(obj.status == 401 || obj.status == 403){
var message = 'Could not login. Please check your '+
'<a href="#" onclick="displaySettings()" style="color:red;text-decoration:underline">login information!</a>'
showError(message)
document.getElementById('favorites').innerHTML = message
}
else if(obj.status == 503){
showError('Error 503: Please try again..')
}
else{
showError(obj.status + ' ' + obj.responseText);
}
}
}
function getLastUpdateTime(){
var url = baseURL + 'posts/update?' + Math.round(Math.random() * 1000)
showError('Checking for updates...')
loadXMLDoc(url, 'getLastUpdateTime', onResponseGetLastUpdateTime)
}
function onResponseGetLastUpdateTime(){
if( ! checkReadyState(xmlhttp) ){
return
}
var responseXML = xmlhttp.responseXML
if( responseXML == null ){
showError('Error loading XML')
return
}
try{
var lastUpdateTime = responseXML.getElementsByTagName("update")[0].getAttribute('time')
} catch(err){
showError('bad response getting time...')
alert(xmlhttp.responseText)
return
}
var previousUpdateTime = readSetting('UpdateTime')
if( previousUpdateTime != lastUpdateTime){
showError('Update available')
writeSetting('UpdateTime', lastUpdateTime)
window.setTimeout('downloadFavorites()', 1000)
}else {
showError('No favorite updates')
}
}
function viewFavorites(){
var html = unescape( readFile(favoritesCache) )
if(html == null || html == ''){
writeSetting('UpdateTime', '')
return
}
favoritesHTML = html
currentlyOpenFolder = null
document.getElementById('favorites').innerHTML = html
}
function downloadFavorites(){
var url = baseURL + 'posts/all?' + + Math.round(Math.random() * 1000)
showError('Updating...')
loadXMLDoc(url, 'viewFavorites', onResponseViewFavorites)
}
function onResponseViewFavorites(){
if( ! checkReadyState(xmlhttp) ){
return
}
var responseXML = xmlhttp.responseXML
if( responseXML == null ){
showError('Error loading XML')
return
}
var response = ''
posts = new Array()
items = responseXML.getElementsByTagName("post");
if(items && items.length != 0 ){
for(var j=0; j < items.length; j++) {
addPost(items[j], posts)
}
}
else{
response += '<b>No favorites found</b>'
}
var tags = new Array()
addPostsToTags( posts, tags )
if(sortTags == '1'){
tags.sort()
}
response += writeTree( tags )
writeFile(favoritesCache, escape( response ))
favoritesHTML = response
showError('Favorites Downloaded')
document.getElementById('favorites').innerHTML = response
currentlyOpenFolder = null
document.getElementById('searchbox').value = ''
}
function addPostsToTags( postsArray, tagsArray ){
for(var i=0; i<postsArray.length; i++){
var tags = postsArray[i].tag.toLowerCase()
var tagArray = tags.split(' ')
for(var j=0; j<tagArray.length; j++){
var position = inArray(tagArray[j], tagsArray)
if( position >= 0 ){
tagsArray[position][1].push( postsArray[i] )
} else {
tagsArray.push(new Array( tagArray[j], new Array( postsArray[i] ) ) )
}
}
}
}
function writeTree( tagsArray ){
var html = ''
var unfiledHtml = ''
for( var i=0; i<tagsArray.length; i++ ){
var tag = tagsArray[i][0]
if(tag == 'system:unfiled'){
unfiledHtml = addLinks( tagsArray[i][1] )
continue
}
html += '<div class="trigger" onclick="expandCollapse(' + i + ')" oncontextmenu="showContextMenu(this);return false" '+
'onmouseover="this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'">'+
'<img src="images/folderClosed.gif" border="0" id="folder' + i + '">'+
tag +
'</div>\n<div class="branch" id="branch' + i + '">'
html += addLinks( tagsArray[i][1] )
html += '</div>\n'
}
html += unfiledHtml
return html
}
function addLinks(items){
var html = ''
for(var i=0; i<items.length; i++){
var item = items[i]
html += '<div class=linkDiv oncontextmenu="showContextMenu(this);return false">'+
'<a class=link href="' + item.href + '" target=_blank title="' + item.tooltip + '" '+
'onmouseover="showLink(this.href)" onmouseout="hideLink()" '+
'extended="' + item.extended + '" tag="' + item.tag + '" hashCode="' + item.hash + '">'+
'<img src="images/page.gif">' +
item.description +
'</a>'+
'</div>\n'
}
return html
}
function addPost( item , array){
var href = item.getAttribute('href')
var description = item.getAttribute('description')
var tag = item.getAttribute('tag')
var time = item.getAttribute('time').substring(0,10)
var hash = item.getAttribute('hash')
var extended = item.getAttribute('extended')
if(extended == null){
extended = ''
}
if(showTooltip == '1'){
var tooltip = 'Added: ' + time
var MAX_DESCRIPTION_LENGTH = 30
if(description.length > MAX_DESCRIPTION_LENGTH){
tooltip = description + '\n' + tooltip
}
if(extended){
tooltip += '\nNote: ' + extended
}
} else {
var tooltip = ''
}
array.push( {'href':href,
'description':description,
'tag':tag,
'time':time,
'hash':hash,
'extended':extended,
'tooltip':tooltip
} )
}
function onResponsePostFavorite(){
if( ! checkReadyState(xmlhttp) ){
return
}
var responseXML = xmlhttp.responseXML
if( responseXML == null ){
showError('Error loading XML')
return
}
try{
var response = responseXML.getElementsByTagName("result")[0].getAttribute('code')
} catch(err){
showError('bad response Posting...')
alert(xmlhttp.responseText)
return
}
if(response == 'done'){
showError('Favorite added!')
window.setTimeout('getLastUpdateTime()', 1000)
}
}
function deleteFavorite(url){
var url = baseURL + 'posts/delete?url=' + encodeURIComponent( url )
showError('deleting...')
loadXMLDoc(url, 'deleteFavorite', onResponseDeleteFavorite)
}
function onResponseDeleteFavorite(){
if( ! checkReadyState(xmlhttp) ){
return
}
var responseXML = xmlhttp.responseXML
if( responseXML == null ){
showError('Error loading XML')
return
}
try{
var response = responseXML.getElementsByTagName("result")[0].getAttribute('code')
} catch(err){
showError('bad response Deleting...')
alert(xmlhttp.responseText)
return
}
if(response == 'done'){
showError('Favorite deleted!')
window.setTimeout('getLastUpdateTime()', 1000)
} else {
showError('Error: ' + response)
}
}
function getCurrentWebsiteURL(){
return external.get_tab(SECURITY_ID, external.cur_sel).document.URL
}
function getCurrentWebsiteTitle(){
return external.get_tab(SECURITY_ID, external.cur_sel).document.title
}
function getEditWebsiteURL(){
return editElement.href
}
function getEditWebsiteTitle(){
return editElement.innerText
}
function getEditWebsiteNotes(){
return editElement.extended
}
function getEditWebsiteTags(){
return editElement.tag
}
function displaySettings(){
document.getElementById('favorites').style.display = 'none'
document.getElementById('settings').style.display = ''
document.getElementById('message').style.display = 'none'
}
function hideSettings(){
document.getElementById('favorites').style.display = ''
document.getElementById('settings').style.display = 'none'
document.getElementById('message').style.display = 'none'
}
function cancel(){
hideSettings()
if(userName == '' || password == ''){
var message = '<a href="javascript:displaySettings()">Login first!</a>'
document.getElementById('favorites').innerHTML = message
}
}
function save(){
userNameNew = encodeURIComponent( document.getElementById('userName').value )
passwordNew = encodeURIComponent( document.getElementById('password').value )
writeSetting('userName', encodeTxt( userNameNew ))
writeSetting('password', encodeTxt( passwordNew ))
if(userName != userNameNew){
writeFile(favoritesCache, '')
writeSetting('UpdateTime', '')
}
location.reload()
}
function setSettingsForm(){
document.getElementById('userName').value = userName
document.getElementById('password').value = password
}
function inArray( text, array ){
for(var i=0; i<array.length; i++){
if(text == array[i][0]){
return i
}
}
return -1
}